﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.ServiceLocation;
using Subfaculty.Common.Interfaces.Services;
using Subfaculty.WebApplication.Translators;
using Subfaculty.WebApplication.ViewModels;
using Telerik.Web.Mvc;
using Subfaculty.Common.Entities;

namespace Subfaculty.WebApplication.Controllers
{
    public class StudyGroupController : Controller
    {
    	private IStudyGroupRepository _studyGroupRepository;
    	private IStudyAreaRepository _studyAreaRepository;
    	private IStudyYearRepository _studyYearRepository;
    	private StudyGroupTranslator _translators;
    	//
        // GET: /StudyGroup/
		public StudyGroupController()
		{
			_studyYearRepository = ServiceLocator.Current.GetInstance<IStudyYearRepository>();
			_studyAreaRepository = ServiceLocator.Current.GetInstance<IStudyAreaRepository>();
			_studyGroupRepository = ServiceLocator.Current.GetInstance<IStudyGroupRepository>();
			_translators = new StudyGroupTranslator();
		}

		public ActionResult Index()
		{

			PopulateStudyArea();
			PopulateStudyYear();
			ViewBag.Message = "Список дисциплин";

			var studyGroupPe = _translators.Translate(_studyGroupRepository.GetAll());

			return View(studyGroupPe);
		}

		[GridAction]
		public ActionResult _Index()
		{

			PopulateStudyArea();
			PopulateStudyYear();
			var studyGroupPe = _translators.Translate(_studyGroupRepository.GetAll());

			return View("Index", new GridModel(studyGroupPe));
		}

		[GridAction]
		public ActionResult SelectAjaxEditing()
		{

			PopulateStudyArea();
			PopulateStudyYear();
			var studyGroupPe = _translators.Translate(_studyGroupRepository.GetAll());

			return View(new GridModel(studyGroupPe));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult SaveAjaxEditing(int id)
		{
			StudyGroup studyGroup = _studyGroupRepository.Get(id);
			StudyGroupViewModel studyGroupPe = _translators.Translate(studyGroup);

			TryUpdateModel(studyGroupPe);

			_studyGroupRepository.Save(_translators.TranslateBack(studyGroupPe, studyGroup));

			var studyGroupPeList = _translators.Translate(_studyGroupRepository.GetAll());

			PopulateStudyArea();
			PopulateStudyYear();
			return View("Index", new GridModel(studyGroupPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult InsertAjaxEditing(StudyGroupViewModel st)
		{
			StudyGroupViewModel studyGroupPe = new StudyGroupViewModel();

			studyGroupPe = st;

			//Perform model binding (fill the product properties and validate it).
			if (TryUpdateModel(studyGroupPe, null, null, new[] {"StudyAreaId", "StudyYearId"}))
			{
				StudyGroup studyGroup = new StudyGroup();
				_translators.TranslateBack(studyGroupPe, studyGroup);

				for (int i = 0; i < studyGroup.StudyArea.Duration; i++ )
				{
					var groupState = new StudyGroupState { Name = "Нет имени", Semester = i++, GroupState = EGroupState.Disband, Group = studyGroup };
					studyGroup.StudyGroupStates.Add(groupState);
				}
				
				//The model is valid - insert the product.
				_studyGroupRepository.Save(studyGroup);
			}
			//Rebind the grid
			var studyGroupPeList = _translators.Translate(_studyGroupRepository.GetAll());

			PopulateStudyArea();
			PopulateStudyYear();
			return View("Index", new GridModel(studyGroupPeList));
		}
		[AcceptVerbs(HttpVerbs.Post)]
		[GridAction]
		public ActionResult DeleteAjaxEditing(int id)
		{
			//Find a customer with ProductID equal to the id action parameter
			StudyGroup studyGroup = _studyGroupRepository.Get(id);
			if (studyGroup != null)
			{
				//Delete the record 
				_studyGroupRepository.Delete(studyGroup);
			}

			var studyGroupPeList = _translators.Translate(_studyGroupRepository.GetAll());
			//Rebind the grid
			PopulateStudyArea();
			PopulateStudyYear();
			return View("Index", new GridModel(studyGroupPeList));
		}

		public ActionResult ShowStudyGroupState(int id)
		{

			var studyGroupPe = new StudyGroupViewModel()
			                   	{
			                   		Id = 1,
									StudyArea = "230100 Информатика и вычислительная техника Вычислительные машины и системы",
			                   		StudyAreaId = 1,
									StudyYear = "2009 - 2010",
			                   		StudyYearId = 1,
			                   		StudyGroupStates = new List<StudyGroupStateViewModel>
			                   		{	                   	
			                   		                   		
			                   		                   					new StudyGroupStateViewModel()
			                   		                   						{
			                   		                   							Semester = 1,
			                   		                   							Name = "ЭВМ-5.1",
			                   		                   							CountSubgroup = 2,
			                   		                   							CountStudent = 20,
			                   		                   							GroupState = EGroupState.Active
			                   		                   						},
																						                   		                   		
			                   		                   					new StudyGroupStateViewModel()
			                   		                   						{
			                   		                   							Semester = 2,
			                   		                   							Name = "ЭВМ-5.1",
			                   		                   							CountSubgroup = 2,
			                   		                   							CountStudent = 19,
			                   		                   							GroupState = EGroupState.Active
			                   		                   						},
																						                   		                   		
			                   		                   					new StudyGroupStateViewModel()
			                   		                   						{
			                   		                   							Semester = 3,
			                   		                   							Name = "ЭВМ-6.1",
			                   		                   							CountSubgroup = 1,
			                   		                   							CountStudent = 12,
			                   		                   							GroupState = EGroupState.Active
			                   		                   						},
																						                   		                   		
			                   		                   					new StudyGroupStateViewModel()
			                   		                   						{
			                   		                   							Semester = 4,
			                   		                   							Name = "ЭВМ-6.1",
			                   		                   							CountSubgroup = 1,
			                   		                   							CountStudent = 11,
			                   		                   							GroupState = EGroupState.Active
			                   		                   						},
			                   		                   		
									}
			                   		};

			return View("GroupState", studyGroupPe);
		}

		private void PopulateStudyArea()
		{
			ViewBag.StudyAreas = _studyAreaRepository.GetAll().Select( e => new SelectListItem { Text = e.Name, Value = e.Id.ToString()});

		}

		private void PopulateStudyYear()
		{
			ViewBag.StudyYears = _studyYearRepository.GetAll().Select(e => new SelectListItem { Text = e.StartDate + @" - " + e.EndDate, Value = e.Id.ToString() });
		}
    }
}
